<?php

class MatchRecordDao{
    function MatchRecordDao(){
        
    }
    
    function load($recordId) {
        $mr = new MatchRecord();
        $rs = executeQuery("select match_id, team_id, score, casualties, winnings, fanfactor, comments, rerolls, coaches, cheerleaders, apothecary from match_records where match_record_id=$recordId");
        $row = mysql_fetch_row($rs);
        $i = 0;
        $mr->id = $recordId;
        $mr->matchId = $row[$i++];
        $mr->teamId = $row[$i++];
        $mr->score = $row[$i++];
        $mr->casualties = $row[$i++];
        $mr->winnings = getZeroForNull($row[$i++]);
        $mr->fanFactor = getZeroForNull($row[$i++]);
        $mr->comments = stripslashes($row[$i++]);
        $mr->rerolls = getZeroForNull($row[$i++]);
        $mr->coaches = getZeroForNull($row[$i++]);
        $mr->cheerleaders = getZeroForNull($row[$i++]);
        $mr->apothecary = getZeroForNull($row[$i++]);
        return $mr;
    }
    
    function create($match, $matchRecord) {
    	$comments = "";
    	if (strlen($matchRecord->comments) > 0) {
			$comments = addslashes($matchRecord->comments);    	    
    	}
    	
        $rs = executeQuery("insert into match_records (match_record_id, match_id, team_id, score, casualties, winnings, " .
        		"fanfactor, comments, rerolls, coaches, cheerleaders, apothecary, tv) " .
        		"values ($matchRecord->id, $match->id, $matchRecord->teamId, $matchRecord->score, $matchRecord->casualties, " .
        		"$matchRecord->winnings, $matchRecord->fanFactor, '$comments', $matchRecord->rerolls, $matchRecord->coaches, " .
        		"$matchRecord->cheerleaders, $matchRecord->apothecary, $matchRecord->tv)");
        return $rs;
    }
}

class MatchRecord {
    var $id;
    var $matchId;
    var $teamId;
    var $score;
    var $casualties;
    var $winnings;
    var $fanFactor;
    var $comments;
    var $rerolls;
    var $apothecary;
    var $coaches;
    var $cheerleaders;
    var $purchases; // Lazy
    var $tv;

	function MatchRecord() {
        $this->score = 0;
        $this->casualties = 0;
        $this->winnings = 0;
        $this->fanFactor = 0;
        $this->comments = "";
        $this->rerolls = 0;
        $this->coaches = 0;
        $this->cheerleaders = 0;
        $this->apothecary = 0;
        $this->tv = 0;
	}
    
    /* Assumes all values have been set */
    function update() {
        $comments = addslashes($this->comments);
        $query = "update match_records set team_id=$this->teamId, score=$this->score, casualties=$this->casualties, winnings=$this->winnings,  fanfactor=$this->fanFactor,  comments='$comments', rerolls=$this->rerolls, coaches=$this->coaches, cheerleaders=$this->cheerleaders, apothecary=$this->apothecary where match_record_id=$this->id";
        $rs = executeQuery($query);
        if (isset($this->purchases)) {
	        executeQuery("delete from match_record_player_purchases where fk_match_record_id=$this->id");
            foreach($this->purchases as $mpp) {
            	$pid = $mpp->position->id;
                executeQuery("insert into match_record_player_purchases (fk_match_record_id, fk_position_id, roster_num, name) values ($this->id, $pid, $mpp->rosterNum, '$mpp->name')");
            }
        }
        return $rs;
    }

	/* Lazily look up the purchases */
    function getPurchases() {
        global $teams;
        if (!isset ($this->purchases)) {
	        $team = $teams->getTeam($this->teamId);
            $this->purchases = array ();
            $rs = executeQuery("select fk_position_id, roster_num, name from match_record_player_purchases where fk_match_record_id=$this->id");
            while ($row = mysql_fetch_row($rs)) {
                $mpp = new MatchPositionPurchase();
                
                $mpp->init($row, $team->race);
                array_push($this->purchases, $mpp);
            }
        }
        return $this->purchases;
    }
}
?>